<?php

namespace app\common\service;


class Host extends Base 
{
    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/10/24 13:55
     */
    function modify($condition, $updateData)
    {
        return db('host')->where($condition)->update($updateData);
    }
    /*
     * @name: 系统比率
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-06-04 14:00:04
     */
    function osStatistics()
    {
        $osRelation = model('Host')->group('os')->column('count(*)', 'os');
        $return = [];
        foreach ($osRelation as $k => $v)
        {
            $child = [
                'name' => model('Host')->showOs($k),
                'value' => $v,
            ];
            $return[] = $child;
        }
        
        return $return;
    }

    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-24 15:48:17
     */
    function list($data, $limit = 10)
    {

        //只能查看当前用户组的主机
        $Host = db('Host')->alias('h')
            ->join('host_link hl', 'h.id = hl.host_id')
            ->join('host_module_relation hmr', 'h.id = hmr.host_id')
            ->where('module', 'client')
            ->whereIn('h.id', $data['id']);

        //本表查询
        isset($data['alias']) && !empty($data['alias']) ? $Host->where('h.id', $data['alias']) : '';//资产别名
        isset($data['os']) && !empty($data['os']) ? $Host->where('h.os', $data['os']) : '';//系统类型
        isset($data['ip']) && !empty($data['ip']) ? $Host->whereLike('h.ip', '%' . $data['ip'] . '%') : '';
        isset($data['description']) && !empty($data['description']) ? $Host->whereLike('h.description', '%' . $data['description'] . '%') : '';


        $field = 'h.id, h.name as alias, h.system_name, h.ip, h.os,h.description,h.client_version,          
          hl.status as client_online_status, h.is_software, hmr.wipf as wipf_stauts, hmr.defense as defense_status';
        $Host->field($field);
        $return = $Host->order('h.id', 'DESC')->paginate($limit)->toArray();

        return $return;
    }

    /**
     * @name: 详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-25 11:24:18
     */
    function info ($host_id)
    {
        $return = db('Host')->alias('h')->find($host_id);

        return $return;
    }

    /**
     * @name: 添加
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 10:36:52
     */
    function add ($data)
    {
        $data['ctime'] = time();
        $return = model('Host')->insertGetId($data);

        return $return;
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 13:21:29
     */
    function uninstall ($host_id_a)
    {

        db('host')->whereIn('id', $host_id_a)->delete();
    }

    /**
     * @name: 主机属性列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-15 10:51:36
     */
    function hostList ($host_id_a, $field)
    {
        $return = db('Host')->whereIn('id', $host_id_a)->orderRaw("field(id," . implode(',', $host_id_a) . ")")->column($field, 'id');
        
        return $return;
    }

    /**
     * @name: 查找通过id
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-15 16:18:50
     */
    function findById ($host_id)
    {
        $return = db('Host')->find($host_id);

        return $return;
    }

    /**
     * @name: 标签比率
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-20 17:07:20
     */
    function tagStatistics ()
    {
        $HostTag = new HostTag();
        $tag_a = $HostTag->showTag([], 'name', 'id');
        $return = [];
        foreach ($tag_a as $k => $v)
        {
            $return[] = [
                'name' => $v,
                'value' => db('Host')->where('FIND_IN_SET(' . "'" . $k . "'" . ',tag)')->count(),
            ]; 
        }

        return $return;
    }

    /**
     * @name: 未添加模块
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-02 15:01:09
     */
    function notAddedModule ($module, $filterData, $limit)
    {

        $HostModuleRelation = new HostModuleRelation();
        $field_a = $HostModuleRelation->getFields();
        if(!array_search($module, $field_a))
        {
            $this->setError($this->CODE_FAIL, '未找到该模块信息');
            return false;
        }



        $Host = db('Host')->alias('h');
        $Host->join('HostModuleRelation hmr', 'hmr.host_id = h.id')
            ->join('HostLink hl', 'h.id=hl.host_id')
            ->where('hmr.'.$module, 0)->where('hl.module', 'client')->where('os', '>', 0);
        !isset($filterData['name']) || empty($filterData['name']) ? : $Host->whereLike('h.name', '%' . $filterData['name'] . '%');
        !isset($filterData['ip']) || empty($filterData['ip']) ? : $Host->whereLiKe('h.ip', '%' . $filterData['ip'] . '%');
        !isset($filterData['online']) || !is_numeric($filterData['online']) ? : $Host->where('hl.status', $filterData['online']);


        //模块冲突
        if($module == 'netfilter'){
            $Host->where('hgshield', '<>',1);
        }elseif($module == 'disguise'){

            $Host->where('netfilter = 1 or hgshield = 1');

        }elseif ($module == 'hgshield'){

            $Host->where('netfilter','<>',1);
        }

        $Host->field('h.id,h.name,h.alias,h.ip,h.system_name,hl.status online');
        $return = $Host->order('h.id', 'DESC')
        ->paginate($limit)->toArray();

        return $return;
    }


    /**
     * @name: 存活主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-03 16:25:16
     */
    function existNum()
    {
        $return = db('Host')->count();

        return $return;
    }

    /**
     * @name 通过host_id获取ip
     * @param $host_id
     * @return mixed
     * @author wx
     * @date 2019/12/25 10:57
     */
    function GetHostIp($host_id)
    {
        return db('Host')->where('id',$host_id)->value('ip');
    }

    /**
     * @name 根据条件查询一条数据
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     * @Author wx
     * @date 2019/8/14 14:03
     */
    function findData($condition, $field = '*')
    {
        return  db('host')->field($field)->where($condition)->find();
    }

    /**
     * @name 根据条件查询所有主机信息
     * @param $condition
     * @param string $field
     * @return array|null|\PDOStatement|string|\think\Model
     * @Author wx
     * @date 2019/8/14 14:21
     */
    function getHostINfo($condition, $field = '*')
    {
        $info = db('host')->alias('h')
            ->join('host_status hs','h.id = hs.host_id')
            ->join('ips_config ic', 'h.id = ic.host_id')
            ->field($field)->where($condition)->find();

        return $info;
    }

    /**
     * @name 所有主机
     * @author wx
     * @date 2020/5/25 15:25
     */
    function filterData()
    {
        return db('host')->field('id, name, ip, system_name')->select();
    }




}